#ifndef DATAANALYSIS_H
#define DATAANALYSIS_H

#include <QWidget>
#include <QtCharts>
#include <QDateTime>
#include "databasemanager.h"

class QPrinter;
class QPainter;

QT_CHARTS_USE_NAMESPACE

namespace Ui {
class DataAnalysis;
}

class DataAnalysis : public QWidget
{
    Q_OBJECT

public:
    explicit DataAnalysis(QWidget *parent = nullptr);
    ~DataAnalysis();

private slots:
    void onDeviceSelectionChanged(int index);
    void onTimeRangeChanged();

    void exportChart();
    void onAnalyzeButtonClicked();

public slots:
    void updateCharts();
    void loadDeviceList();

private:
    Ui::DataAnalysis *ui;
    QChart *m_temperatureChart;
    QChart *m_humidityChart;
    QChart *m_alertChart;
    QDateTime m_startTime;
    QDateTime m_endTime;
    int m_currentDeviceId;
    DatabaseManager* dbManager;

    void setupCharts();
    void loadTemperatureData();
    void loadHumidityData();
    void loadAlertData();
    void performTrendAnalysis();
    void setupConnections();
    void clearAllCharts();
    QVector<QPair<QDateTime, double>> fetchData(const QString& field);
    QVector<QPair<QString, int>> fetchAlertStats();
};

#endif // DATAANALYSIS_H
